﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace SpeechRecognitionTest
{
  class Program
  {
    static bool completed;

    static void Main(string[] args)
    {
      using (SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine())
      {
        while (true)
        {
          TestSpeechRecog(recognizer);
        }
      }

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }

    static void TestSpeechRecog(SpeechRecognitionEngine recognizer)
    {
      // Create and load a grammar.
      Grammar dictation = new DictationGrammar();
      dictation.Name = "Dictation Grammar";

      recognizer.LoadGrammar(dictation);
      // Configure the input to the recognizer.
      //recognizer.SetInputToWaveFile(@"E:\VSWork\ClickRead\ClickRead\res\audio\1_1_2.wav");
      recognizer.SetInputToDefaultAudioDevice();

      // Attach event handlers for the results of recognition.
      recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
      recognizer.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);
      recognizer.AudioSignalProblemOccurred += new EventHandler<AudioSignalProblemOccurredEventArgs>(recognizer_AudioSignalProblemOccurred);
      recognizer.SpeechRecognized +=
        new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
      recognizer.RecognizeCompleted +=
        new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);

      // Perform recognition on the entire file.
      Console.WriteLine("Starting asynchronous recognition...");
      completed = false;
      recognizer.RecognizeAsync();

      // Keep the console window open.
      while (!completed)
      {
        Console.ReadLine();
      }
      Console.WriteLine("Done.");
    }

    static void recognizer_AudioSignalProblemOccurred(object sender, AudioSignalProblemOccurredEventArgs e)
    {
      
    }

    static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
    {
      
    }

    static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)
    {
      
    }

    // Handle the SpeechRecognized event.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      if (e.Result != null && e.Result.Text != null)
      {
        Console.WriteLine("  Recognized text =  {0}", e.Result.Text);
      }
      else
      {
        Console.WriteLine("  Recognized text not available.");
      }
    }

    // Handle the RecognizeCompleted event.
    static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
    {
      if (e.Error != null)
      {
        Console.WriteLine("  Error encountered, {0}: {1}",
        e.Error.GetType().Name, e.Error.Message);
      }
      if (e.Cancelled)
      {
        Console.WriteLine("  Operation cancelled.");
      }
      if (e.InputStreamEnded)
      {
        Console.WriteLine("  End of stream encountered.");
      }
      completed = true;
    }
  }
}
